<?
	
	class EmailView extends View{
		
		var $_headers;
		var $_protocol="mail";//mail,smtp
		var $_sep="\r\n";
		
		var $smtp_host = "localhost";
		var $smtp_port = 25;
		
		
		var $email_params=array();
		var $is_notification=false;
		
		function EmailView($template_path=null){
			if($template_path==null) $template_path = pathjoin(ROOT,getconf("front_template_path"));
			$this->tempalte_path = $template_path;
			$this->_protocol = getconf("email_protocol");
			$this->file="email.php";
		}
		
		function setHeaders($headers){
			$hs = array();
			foreach($headers as $k=>$v) $hs[ucfirst(trim($k))]=$v;
			$headers = $hs;
			$this->headers=$headers;
			
		}
		
		function setSubject($subject){
			$this->headers["Subject"]=$subject;
		}
		
		function setFrom($from){
			$this->headers["From"]=$from;
		}
		
		function setTo($to){
			$this->headers["To"]=$to;
		}
		
		function from($from){
			$this->setFrom($from);
		}
		
		function to($to){
			$this->setTo($to);
		}
		
		function replyto($replyto){
			$this->headers["Reply-To"]=$replyto;
		}
		
		
		
		
		function send($view_file,$langcode=NULL){
			
			
			if(getlang()!=DEF_LANG){
				$content_lang_file=pathjoin(dirname($view_file),basename($view_file,"php")."lang",getlang().".php");
				if(file_exists($content_lang_file)) $this->parseLang($content_lang_file);
			}
			
			global $__view;
			$back_view = $__view;
			$__view=$this;
			$this->parse($view_file);
			$__view=$back_view;
			
			
			if(!$this->is_notification){
				$this->headers["Subject"]=trim($this->subject);
			}else{
				$this->headers["Subject"]="Notification -- ".trim($this->subject);
			}
			$content = parent::render($this->tempalte_path."/".$this->file);
			
			
			
			if(!in_array($this->_protocol,array("mail","smtp"))){
				$this->_protocol="mail";
			}
			
			if($this->_protocol=="mail"){
				$headers=$this->headers;
				unset($headers["To"]);
				unset($headers["Subject"]);
				$headers = array_merge(
					array(	"MIME-Version"=>"1.0",
							"Content-type"=>"text/html; charset=utf-8"),
					$headers
				);
				
				$header_string="";
				foreach($headers as $k=>$value){
					$header_string.="{$k}: $value".$this->_sep;
				}
				
				mail($this->headers["To"],$this->headers["Subject"],$content,$header_string);
			}
			if($this->_protocol=="smtp"){
				
				$headers=$this->headers;
				
				$headers = array_merge(
					array(	"MIME-Version"=>"1.0",
							"Content-type"=>"text/html; charset=utf-8"),
					$headers
				);
				
				//connect to the host and port
				$smtpConnect = fsockopen($this->smtp_host, $this->smtp_port, $errno, $errstr, 30);
				$smtpResponse = fgets($smtpConnect, 4096);
				if(empty($smtpConnect))
				{
				$output = "Failed to connect: $smtpResponse";
				return $output;
				}
				else
				{
				$logArray['connection'] = "Connected to: $smtpResponse";
				}
				
				//say HELO to our little friend
				fputs($smtpConnect, "HELO 127.0.0.1". $this->_sep);
				$smtpResponse = fgets($smtpConnect, 4096);
				$logArray['heloresponse'] = "$smtpResponse";
				
				
				
				//email from
				fputs($smtpConnect, "MAIL FROM: {$this->headers["From"]}" . 	$this->_sep);
				$smtpResponse = fgets($smtpConnect, 4096);
				$logArray['mailfromresponse'] = "$smtpResponse";
				
				
				//email to
				fputs($smtpConnect, "RCPT TO: {$this->headers["To"]}" . $this->_sep);
				$smtpResponse = fgets($smtpConnect, 4096);
				$logArray['mailtoresponse'] = "$smtpResponse";
				
			
				
				
				//the email
				fputs($smtpConnect, "DATA" . $this->_sep);
				$smtpResponse = fgets($smtpConnect, 4096);
				$logArray['data1response'] = "$smtpResponse";
				
				//header
				$header_string="";
				foreach($headers as $k=>$value){
					$header_string.="{$k}: $value".$this->_sep;
				}
				
				//observe the . after the newline, it signals the end of message
				fputs($smtpConnect, "{$header_string}{$this->_sep}{$this->_sep}$content{$this->_sep}.{$this->_sep}");
				$smtpResponse = fgets($smtpConnect, 4096);
				$logArray['data2response'] = "$smtpResponse";
				
				// say goodbye
				fputs($smtpConnect,"QUIT" . $this->_sep);
				$smtpResponse = fgets($smtpConnect, 4096);
				$logArray['quitresponse'] = "$smtpResponse";
				$logArray['quitcode'] = substr($smtpResponse,0,3);
				fclose($smtpConnect);
				//a return value of 221 in $retVal["quitcode"] is a success
				
				return($logArray);
				
			}
		}
		

		
	}

?>
